define([
    'jquery',
    'underscore',
    'backbone',
    'text!module/operation/templates/rpt_purcharse.html',
    'text!module/operation/templates/rpt_purcharse_supplier.html',
    'text!module/operation/templates/rpt_sale.html',
    'text!module/operation/templates/rpt_sale_client.html'
], function($, _, Backbone, rpt_purcharse, rpt_purcharse_supplier, rpt_sale, rpt_sale_client) {

    var ReportArticleController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
        },
        events: {
            "click #report_purcharse_generate": "report_purcharse_generate",
            "click #report_purcharse_pdf": "report_purcharse_pdf",
            "click #report_purcharse_excel": "report_purcharse_excel",
            "click #purcharse_supplier_generate": "purcharse_supplier_generate",
            "click #purcharse_supplier_pdf": "purcharse_supplier_pdf",
            "click #purcharse_supplier_excel": "purcharse_supplier_excel",
            "click #report_sale_generate": "report_sale_generate",
            "click #report_sale_pdf": "report_sale_pdf",
            "click #report_sale_excel": "report_sale_excel",
            "click #report_sale_client_generate": "report_sale_client_generate",
            "click #report_sale_client_pdf": "report_sale_client_pdf",
            "click #report_sale_client_excel": "report_sale_client_excel",
        },
        render: function() {

        },
        report_purcharse: function() {
            this.$el.html(rpt_purcharse);
            $('#date').datepicker({
                changeMonth: true,
                changeYear: false,
                dateFormat: 'MM', //'MM yy'
                showButtonPanel: true,
                onClose: function() {
                    var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                    var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                    $(this).val(iMonth + 1);
                    //$(this).datepicker('setDate', new Date(iYear, iMonth, 1));
                },
                beforeShow: function() {
                    /*if ((selDate = $(this).val()).length > 0)
                     {
                     iYear = selDate.substring(selDate.length - 4, selDate.length);
                     iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5),
                     $(this).datepicker('option', 'monthNames'));
                     $(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
                     $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
                     }*/
                }
            });
        },
        report_purcharse_generate: function() {
            $.get('server/api/oper_rep/pur/' + $("#date").val(), function(data) {
                $("#rst_table tbody").html("");
                $.each(data, function(i, item) {
                    
                    $("#rst_table tbody").append(
                            "<tr>" +
                            "<td>" + paddy(parseInt(item.serie), 3) + " - " + item.number + "</td>" +
                            "<td>" + item.date + "</td>" +
                            "<td>" + item.code + "</td>" +
                            "<td>" + item.name + "</td>" +
                            "<td>" + item.subtotal + "</td>" +
                            "<td>" + item.tax + "</td>" +
                            "<td>" + item.total + "</td>" +
                            "</tr>");
                });
            }, "json");
        },
        report_purcharse_pdf: function() {
            relocate("server/reports/report_operator.php", {
                report: "report_purcharse_pdf",
                month: $("#date").val()
            });
        },
        report_purcharse_excel: function() {
            relocate("server/reports/report_operator.php", {
                report: "report_purcharse_excel",
                month: $("#date").val()
            });
        },
        report_purcharse_supplier: function() {
            this.$el.html(rpt_purcharse_supplier);
            $.get('server/api/operator/find/1', function(data) {
                $.each(data, function(i, item) {
                    var option = document.createElement('option');
                    $(option).val(item.id);
                    $(option).html(item.name);
                    $("#operator").append(option);
                });
            }, "json");
        },
        purcharse_supplier_generate: function() {
            $.get('server/api/oper_rep/pur_op/' + $("#operator option:selected").val(), function(data) {
                $("#rst_table tbody").html("");
                $.each(data, function(i, item) {
                    
                    $("#rst_table tbody").append(
                            "<tr>" +
                            "<td>" + paddy(parseInt(item.serie), 3) + " - " + item.number + "</td>" +
                            "<td>" + item.date + "</td>" +
                            "<td>" + item.code + "</td>" +
                            "<td>" + item.name + "</td>" +
                            "<td>" + item.subtotal + "</td>" +
                            "<td>" + item.tax + "</td>" +
                            "<td>" + item.total + "</td>" +
                            "</tr>");
                });
            }, "json");
        },
        purcharse_supplier_pdf: function() {
            relocate("server/reports/report_operator.php", {
                report: "purcharse_supplier_pdf",
                operator_id: $("#operator option:selected").val()
            });
        },
        purcharse_supplier_excel: function() {
            relocate("server/reports/report_operator.php", {
                report: "purcharse_supplier_excel",
                operator_id: $("#operator option:selected").val()
            });
        },
        //-----------------------------------------------------------------------
        report_sale: function() {
            this.$el.html(rpt_sale);
            $('#date').datepicker({
                changeMonth: true,
                changeYear: false,
                dateFormat: 'MM', //'MM yy'
                showButtonPanel: true,
                onClose: function() {
                    var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                    var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                    $(this).val(iMonth + 1);
                    //$(this).datepicker('setDate', new Date(iYear, iMonth, 1));
                },
                beforeShow: function() {
                    /*if ((selDate = $(this).val()).length > 0)
                     {
                     iYear = selDate.substring(selDate.length - 4, selDate.length);
                     iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5),
                     $(this).datepicker('option', 'monthNames'));
                     $(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
                     $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
                     }*/
                }
            });
        },
        report_sale_generate: function() {
            $.get('server/api/oper_rep/sale/' + $("#date").val(), function(data) {
                $("#rst_table tbody").html("");
                $.each(data, function(i, item) {
                    
                    $("#rst_table tbody").append(
                            "<tr>" +
                            "<td>" + paddy(parseInt(item.serie), 3) + " - " + item.number + "</td>" +
                            "<td>" + item.date + "</td>" +
                            "<td>" + item.code + "</td>" +
                            "<td>" + item.name + "</td>" +
                            "<td>" + item.subtotal + "</td>" +
                            "<td>" + item.tax + "</td>" +
                            "<td>" + item.total + "</td>" +
                            "</tr>");
                });
            }, "json");
        },
        report_sale_pdf: function() {
            relocate("server/reports/report_operator.php", {
                report: "report_sale_pdf",
                month: $("#date").val()
            });
        },
        report_sale_excel: function() {
            relocate("server/reports/report_operator.php", {
                report: "report_sale_excel",
                month: $("#date").val()
            });
        },
        report_sale_client: function() {
            this.$el.html(rpt_sale_client);
            $.get('server/api/operator/find/2', function(data) {
                $.each(data, function(i, item) {
                    var option = document.createElement('option');
                    $(option).val(item.id);
                    $(option).html(item.name);
                    $("#operator").append(option);
                });
            }, "json");
        },
        report_sale_client_generate: function() {
            $.get('server/api/oper_rep/sale_op/' + $("#operator option:selected").val(), function(data) {
                $.each(data, function(i, item) {
                    $("#rst_table tbody").html("");
                    $("#rst_table tbody").append(
                            "<tr>" +
                            "<td>" + paddy(parseInt(item.serie), 3) + " - " + item.number + "</td>" +
                            "<td>" + item.date + "</td>" +
                            "<td>" + item.code + "</td>" +
                            "<td>" + item.name + "</td>" +
                            "<td>" + item.subtotal + "</td>" +
                            "<td>" + item.tax + "</td>" +
                            "<td>" + item.total + "</td>" +
                            "</tr>");
                });
            }, "json");
        },
        report_sale_client_pdf: function() {
            relocate("server/reports/report_operator.php", {
                report: "report_sale_client_pdf",
                operator_id: $("#operator option:selected").val()
            });
        },
        report_sale_client_excel: function() {
            relocate("server/reports/report_operator.php", {
                report: "report_sale_client_excel",
                operator_id: $("#operator option:selected").val()
            });
        }
    });
    return ReportArticleController;
});

function relocate(page, params) {
    var body = document.body;
    form = document.createElement('form');
    form.method = 'POST';
    form.action = page;
    form.name = 'jsform';
    for (index in params)
    {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = index;
        input.id = index;
        input.value = params[index];
        form.appendChild(input);
    }
    body.appendChild(form);
    form.submit();
}


function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) {
        i = 0.00;
    }
    var minus = '';
    if (i < 0) {
        minus = '-';
    }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) {
        s += '.00';
    }
    if (s.indexOf('.') == (s.length - 2)) {
        s += '0';
    }
    s = minus + s;
    return s;
}
//var fu = paddy(14, 5); // 00014
//var bar = paddy(2, 4, '#'); // ###2
function paddy(n, p, c) {
    var pad_char = typeof c !== 'undefined' ? c : '0';
    var pad = new Array(1 + p).join(pad_char);
    return (pad + n).slice(-pad.length);
}
